/**
 * Created by cb-dlhis on 17-1-13.
 */
const R = require('ramda');
var env = 'local';
const domainEnv = {
    'localhost': 'local',
    '127.0.0.1': 'local',
    '192.168.199.201': 'local',
    'hummeradmin.prepro.dlhis.com': 'prepro',
    'hummeradmin.lan.dlhis.com': 'prepro'
};
const apiHost = {
    'local': 'http://192.168.199.99:9002',
    'develop': 'http://hummersrv.develop.dlhis.com',
    'prepro': 'http://hummersrv.prepro.dlhis.com'
};
const hostname = window.location.hostname;
if (R.has(hostname, domainEnv)) {
    env = domainEnv[hostname];
}
const APIROOT = apiHost[env];
const URL_OPTIONS = {
    post: {
        method: 'post',
        headers: {'Content-Type': 'application/json'},
        data: {},
        dataType: 'json',
        timeout: 30000
    },
    get: {
        method: 'get',
        headers: {'Content-Type': 'application/json'},
        data: {},
        dataType: 'json',
        timeout: 30000
    }
};
var request = require('superagent-bluebird-promise');
var postRequest = require('superagent');
const GET_INFO = function (relatieURL, fetchCallback) {
    var url = APIROOT + relatieURL;
    url = encodeURI(url);
    return request.get(url)
        .then(res => {
            fetchCallback(res.body, null)
        },error =>{
            fetchCallback(null, error)
        })
};
const PUT_INFO = function (relatieURL, params, cb) {
    var url = APIROOT + relatieURL;
    url = encodeURI(url);
    postRequest
        .post(url)
        .send(params)
        .set('Accept', 'application/json')
        .end(function(err, res) {
            if (err) {
                cb(err, res);
            } else if (res.statusNo != 200) {
                cb(null, res.body);
            } else {
                cb(null, res.body);
            }
        });
};
module.exports = { APIROOT, URL_OPTIONS, GET_INFO, PUT_INFO };
